<?php
class ProductModel
{
	private $product_db;
	private $product_detail_db;
	private $product_file_db;
	private $product_attribute_value_wlp_db;
	private $written_language_id;
	
	public function __construct($category_name,$written_language_tag)
	{
		$language_model = new LanguageModel();
		$this->written_language_id = $language_model->getWrittenLanguageIdByTag($written_language_tag);
		$product_db_class_name = Common::underlineToClassName($category_name).'Db';
		$product_detail_db_class_name = Common::underlineToClassName($category_name).'DetailDb';
		$product_file_db_class_name = Common::underlineToClassName($category_name).'FileDb';
		$product_attribute_value_wlp_db_class_name = Common::underlineToClassName($category_name).'AttributeValueWlpDb';
		$this->product_db = new $product_db_class_name ();
		$this->product_detail_db = new $product_detail_db_class_name ();
		$this->product_file_db = new $product_file_db_class_name ();
		$this->product_attribute_value_wlp_db = new $product_attribute_value_wlp_db_class_name ();
	}
	
	/**
	 * 
	 * @return array(
		'vendor' => array(
			'1' => 'AMD',
			'2' => 'Intel（英特尔）',
		),
		'socket' => array(
			'1' => 'AM3',
			'2' => 'LGA 1155',
		),
		'cores_count' => array(
			'1' => '单核',
			'2' => '双核',
			'3' => '3核',
		)
	)
	 */
	public function getProductAttributeValueWlp()
	{
		$where = array(
			'written_language_id' => $this->written_language_id
		);
		$column = array('attribute_name','attribute_value','display');
		$result = $this->product_attribute_value_wlp_db->selectRows($where,$column);
		$data = array();
		foreach($result as $value)
		{
			$data[$value['attribute_name']][$value['attribute_value']] = $value['display'];
		}
		return $data;
	}
	
	public function getProducts($where)
	{
		$result = $this->product_db->selectRows($where);
		return $result;
	}
	
	public function getProductsId($where,$limit_start,$limit_size,$order_by='')
	{
		$column = array(
			'id'
		);
		$result = $this->product_db->selectRows($where,$column,$limit_start,$limit_size);
		foreach($result as $row)
		{
			$products_id[] = $row['id'];
		}
		return $products_id;
	}
	
	public function getProductsDetail($products_id)
	{
		$where = array(
			'product_id' => $products_id,
			'written_language_id' => $this->written_language_id,
		);
		$column = array(
			'name',
			'product_id',
		);
		$result = $this->product_detail_db->selectRows($where,$column);
		foreach($result as $row)
		{
			$data[$row['product_id']] = $row['name'];
		}
		return $data;
	}
	
	public function getProductsFileUri($products_id,$purpose)
	{
		$where = array(
			'product_id' => $products_id,
			'purpose' => $purpose,
		);
		$column = array(
			'product_id',
			'filename',
		);
		$result = $this->product_file_db->selectRows($where,$column);
		$static_server_uri = ConfigParser::get('system','static_server_uri');
		foreach($result as $row)
		{
			$data[$row['product_id']] = $static_server_uri . $row['filename'];
		}
		return $data;
	}
}
?>
